<?php
if(!defined('IN_DSW')){exit('大神请返回地球，我们需要你');}
/*
 * 错误页面模块 2018-04-17
 */
class action extends app
{
	public function display()
	{
		$action = $this->routes->url(3);
		if(!method_exists($this,$action))
		$action = "manage";
		$this->$action();
		exit;
	}

	private function manage(){
		$page = intval($this->routes->get('page'));
		$page = $page>0?$page:1;
		$ListAll=$this->errorpage->getList($page,10,'','addtime desc');
		$this->tpl->assign('ListAll',$ListAll);
		$this->tpl->assign('pages',$ListAll['pages']);
		$this->tpl->assign("FormAction",MyAppUrl.'index-dels');
		$this->tpl->assign("FormTitle",'错误页面管理列表');
		$this->tpl->display('index');
	}

	private function del(){
		$IntID = intval($this->routes->get('id'));
		$page = intval($this->routes->get('page'));
		$page = $page>0?$page:1;
		$this->errorpage->del(array('id'=>$IntID));
		$message = array(
			'CodeType' =>200,
			"message" => "删除成功",
			"callbackType" => 'forward',
			"forwardUrl" => MyAppUrl."index&page=".$page
		);
		$this->G->R($message);
	}

	private function dels(){
		$SelectCheckbox = $_POST['SelectCheckbox'];
		if(is_array($SelectCheckbox)){
			foreach($SelectCheckbox as $value){
				$this->errorpage->del(array('id'=>$value));
			}
			$message = array(
				'CodeType' =>200,
				"message" => "批量删除成功",
				"callbackType" => 'forward',
				"forwardUrl" => MyAppUrl."index&page=".$page
			);
			$this->G->R($message);
		}else{
			$message = array(
				'CodeType' =>300,
				"message" => "亲，删除是不是需要先选择呢？",
				"callbackType" => 'forward',
				"forwardUrl" => MyAppUrl."index&page=".$page
			);
			$this->G->R($message);
		}		
	}
/**
 * [manage301 301页面跳转]
 * 时间:2018-06-22
 * 作者:牛哥 tinwin@vip.qq.com
 * 功能:让旧网址跳转到新网址上
 */
	private function manage301(){
		$page = intval($this->routes->get('page'));
		$page = $page>0?$page:1;
		$ListAll=$this->errorpage->getList301($page,10,'','addtime desc');
		$this->tpl->assign('ListAll',$ListAll);
		$this->tpl->assign("FormAction",MyAppUrl.'index-dels301');
		$this->tpl->assign("FormTitle",'301页面跳转');
		$this->tpl->display('301_list');
	}

	private function add301(){
		if($_SESSION['input_token']!="" && $_SESSION['input_token']==$_POST['input_token']){
			$_SESSION['input_token']='';
			$dsw =$_POST['dsw'];
			$isnot=$this->errorpage->getOne301(array('errorpage'=>$dsw['errorpage']));
			if(!$isnot&&$dsw){
				$dsw['addtime']=TIME;
				$this->errorpage->add301($dsw);
				$message = array(
					'CodeType' =>200,
					"message" => '添加成功',
					"callbackType" => 'forward',
					"forwardUrl" => MyAppUrl."index-manage310"	
				);
				$this->G->R($message);
			}else{
				$message = array(
					'CodeType' =>300,
					"message" => '已经存在,请勿重复添加',
					"callbackType" => 'forward',
					"forwardUrl" => MyAppUrl."index-manage310"	
				);
				$this->G->R($message);
			}
		}else{
			$_SESSION['input_token'] = md5(rand(100,1000));
			$this->tpl->assign("input_token",$_SESSION['input_token']);
			$this->tpl->assign("FormTitle",'301错误页面跳转');
			$this->tpl->assign("FormAction",MyAppUrl."index-add301");
			$this->tpl->display('301_form');
		}
	}

	private function edit301(){
		$IntID = intval($this->routes->get('id'));
		if($_SESSION['input_token']!="" && $_SESSION['input_token']==$_POST['input_token']){
			$_SESSION['input_token']='';
			$dsw =$_POST['dsw'];
			$isnot=$this->errorpage->getOne301(array('errorpage'=>$dsw['errorpage']));
			if(!$isnot&&$dsw){
				$this->errorpage->update301($dsw,array('id'=>$IntID));
				$message = array(
					'CodeType' =>200,
					"message" => '更新成功',
					"callbackType" => 'forward',
					"forwardUrl" => MyAppUrl."index-manage301"
				);
				$this->G->R($message);
			}
		}else{
			$ListOne=$this->errorpage->getOne301(array('id'=>$IntID));
			$this->tpl->assign("ListOne",$ListOne);
			$_SESSION['input_token'] = md5(rand(100,1000));
			$this->tpl->assign("input_token",$_SESSION['input_token']);
			$this->tpl->assign("FormTitle",'页面编辑');
			$this->tpl->assign("FormAction",MyAppUrl."index-edit301&id=$IntID");
			$this->tpl->display('301_form');
		}
	}

	private function del301(){
		$IntID = intval($this->routes->get('id'));
		$page = intval($this->routes->get('page'));
		$page = $page>0?$page:1;
		$this->errorpage->del301(array('id'=>$IntID));
		$message = array(
			'CodeType' =>200,
			"message" => "删除成功",
			"callbackType" => 'forward',
			"forwardUrl" => MyAppUrl."index-manage301&page=".$page
		);
		$this->G->R($message);
	}

	private function dels301(){
		$page = intval($this->routes->get('page'));
		$page = $page>0?$page:1;
		$SelectCheckbox = $_POST['SelectCheckbox'];
		if(is_array($SelectCheckbox)){
			foreach($SelectCheckbox as $value){
				$this->errorpage->del301(array('id'=>$value));
			}
			$message = array(
				'CodeType' =>200,
				"message" => "批量删除成功",
				"callbackType" => 'forward',
				"forwardUrl" => MyAppUrl."index-manage301&page=".$page
			);
			$this->G->R($message);
		}else{
			$message = array(
				'CodeType' =>300,
				"message" => "亲，删除是不是需要先选择呢？",
				"callbackType" => 'forward',
				"forwardUrl" => MyAppUrl."index-manage301&page=".$page
			);
			$this->G->R($message);
		}		
	}
}
?>
